﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using YJ.Utilities.WindowsService;
using YJ.JobService.Jobs;
using YJ.JobSchedule.Configuration;

namespace YJ.JobService
{
    public partial class AddWinservice : Form
    {
        MainForm mainForm;
        public AddWinservice(MainForm mainForm)
        {
            this.mainForm = mainForm;
            InitializeComponent();
        }

        private async void button2_Click(object sender, EventArgs e)
        {


            if (txtServicePath.Text.IsNullOrWhiteSpace())
            {
                MessageBox.Show("请选择windows服务的安装exe文件!");
                return;
            }
            if (!File.Exists(txtServicePath.Text))
            {
                MessageBox.Show(txtServicePath.Text + ":文件不存在,请检查");
                return;
            }
            if (txtServiceName.Text.IsNullOrWhiteSpace())
            {
                MessageBox.Show("windows服务名称不能为空!");
                return;

            }
            try
            {
                if (WinServiceHelper.IsExists(txtServiceName.Text.Trim()))
                {
                    MessageBox.Show(txtServiceName.Text + ":服务已经存在!");
                    return;
                }
                Loading.ShowLoading();
                WinServiceHelper.Install(txtServicePath.Text);
                WinServiceHelper.SetAuto(txtServiceName.Text);
                WinServiceHelper.Start(txtServiceName.Text);

                //延迟1秒让服务把文件创建起来,TODO:..这种做法不科学
                await Task.Delay(1000);

                //var jobs = JobConfiguationManager.GetAllJobConfiguration().Where(r => r.Group == txtServiceName.Text).ToList();
                //if (jobs == null || jobs.Count == 0)
                //{
                //    Loading.CloseLoading();
                //    MessageBox.Show(txtServiceName.Text + ":该服务不存在Job.Schedule任务,或者没有配置相应的路径,无法启动,请检查!");
                //    WinServiceHelper.UnInstall(txtServiceName.Text);
                //    return;
                //}

                var jobKeys = JobInfoSelfDescriptionAttribute.GetJobKeysFromAssembly(txtServicePath.Text);

                await mainForm.AddWinservice(new WinServices
                {
                    JobKeys = jobKeys,
                    ServiceExePath = txtServicePath.Text,
                    ServiceName = txtServiceName.Text
                });
                
                Loading.CloseLoading();
            }
            catch (Exception ex)
            {
                Loading.CloseLoading();
                MessageBox.Show(txtServiceName.Text + ":服务安装失败!");
                Log4Logger.WriteError(txtServiceName.Text + ":服务安装失败!", ex);
                try
                {
                    WinServiceHelper.UnInstall(txtServiceName.Text);
                }
                catch { }
            }
            Loading.CloseLoading();
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog();
            dialog.Multiselect = false;
            dialog.Filter = "可执行文件(*.exe)|*.exe";

            if (dialog.ShowDialog() == DialogResult.OK)
            {
                string fileName = dialog.FileName;
                txtServicePath.Text = fileName;
            }
        }
    }
}
